<?php 
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");

class M_suppliers extends DB{
	private $dbConn;
	private $rowCount="0";
	private $ID="0";
	private $sqlSelect ="select * from tb_supplier where IsActived=1";
	private $totalpage="0";
	
	function M_suppliers(){
		parent::__construct();
	}
	function getText(){
		echo "TEST";
	}
	function getListPage(){
		global $list_page;
		return $list_page;
	}
	function checkSupplierCode($code){
		$sql="select * from tb_supplier where IsActived=1 and SupplierCode='".$code."'";
		$result=$this->ExecuteData($sql);
		$rows=$result->RecordCount();
		$val=$rows > 0? "0":"1";
		return $val;
	}
	function getData($code=null, $name=null, $term=null,$pageStart=1){
		global $list_page;
		$sqlscript= $this->sqlSelect;
		
		if (isset($code) && $code!=""){
			$sqlscript.= " and SupplierCode='". $code."'";
		}
		if (isset($name) && $name!=""){
			$sqlscript.= " and SupplierName Like '%". $name."%'";
		}
		if (isset($term) && $term !=""){
			$sqlscript.= " and CreditTerm = ". $term."";
		}
		//Order 
		$sqlscript.= " order by ID asc " ;
		//Execute
		$result = $this->ExecuteData($sqlscript);
		//Get all rows
		$rows =$result->RecordCount();
		
		//Page limit
		$sqlscript.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
		
		//paging
		$result = $this->ExecuteData($sqlscript);
		
		$totalpage=1;
		if ($rows>0){
			$rt = $rows%$list_page;
			
			if($rt!=0) {
				$totalpage = floor($rows/$list_page)+1;
			}
			else {
				$totalpage = floor($rows/$list_page);
			}
		}
		
		$this->rowCount=$rows;
		$this->totalpage=$totalpage;
		return $result;
	}
	
	function getDataDetail($id=0){
		$sqlscript= "select ID , SupplierCode , SupplierName, CreditTerm, Address, Telephone,  ContactName, ContactTele,ContactEmail ";
		$sqlscript.= " from tb_supplier where IsActived=1 ";
		$sqlscript.= " and ID='". $id."'";
		$result = $this->ExecuteData($sqlscript);
		return $result->FetchRow();
	}
	
	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->ID;
	}
	
	function InsertData($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript($this->sqlSelect, $val);
			$this->ExecuteData($sqlInsert);
			$this->ID = $this->getLastID();
			return $this->ID;
		}
	}
	function InsertDataDetail($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript("select * from tb_pos_detail where 1=2 ", $val);
			$this->ExecuteData($sqlInsert);
		}
	}
	
	function updateData($sql, $val){
		if(isset($val)){
			$sqlUpdate=$this->UpdateSqlScript($sql,$val);
			return $this->ExecuteData($sqlUpdate);
		}
	}
	function delData($id){
		if(isset($id)){
			$sqlDelete = "update tb_supplier set IsActived=0 where ID='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}
	function getTotalPage(){
		return $this->totalpage;
	}
}
?>